package org.example.mallweb.mallservice.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.mallcommon.projo.Products;
import org.example.mallcommon.result.ReturnResult;
import org.example.mallweb.mallservice.mybatiscommon.products.ProductsMapper;
import org.example.mallweb.mallservice.service.ProductsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 韩先楚
 * 商品服务实现类
 */
@Service
public class ProductServiceImpl implements ProductsService {

    @Resource
    private ProductsMapper productsMapper;

    /**
     * 获取我的商品列表
     */
    @Override
    public List<Products> getMyProducts(Integer userId) {
        List<Products> list = productsMapper.selectList(new QueryWrapper<Products>().eq("user_id", userId));
        return list;
    }

    @Override
    public ReturnResult<String> addProduct(Products products) {
        productsMapper.insert(products);
        return ReturnResult.success("添加成功");
    }

    /**
     * 修改商品信息
     */
    @Override
    public ReturnResult updateProduct(Products products) {
       productsMapper.updateById(products);
       return ReturnResult.success("修改成功");
    }

    /**
     * 查询商品详情
     */
    @Override
    public ReturnResult<Products> getProduct(Integer productId) {
        Products products = productsMapper.selectById(productId);
        return ReturnResult.success(products);
    }

    /**
     * 获取其他用户已上架的商品
     */
    @Override
    public ReturnResult<List<Products>> getOtherProducts(Integer userId) {
        List<Products> products = productsMapper.selectList(new QueryWrapper<Products>().eq("user_id", userId).eq("status", 1));
        return ReturnResult.success(products);
    }


}
